<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_auth{
    private $CI;

    public function __construct(){
        $this->CI =& get_instance();
    }

    public function check(){
        if(isset($_COOKIE[md5(base_url()).'_user_logged']) && !empty($_COOKIE[md5(base_url()).'_user_logged'])){
            $cookie = $_COOKIE[md5(base_url()).'_user_logged'];
            $cookie = json_decode($this->CI->my_string->decode_cookie($cookie),TRUE);
            $user= $this->CI->db->select('ma_nguoidung,ten_nguoidung,matkhau,chuoi_mahoa')->where(array('ten_nguoidung'=>$cookie['ten_nguoidung']))->from('nguoidung')->get()->row_array();
            if(isset($user) && count($user)){
                if($cookie['ten_nguoidung'] == $user['ten_nguoidung'] && $cookie['matkhau'] == $user['matkhau'] && $cookie['chuoi_mahoa'] == $user['chuoi_mahoa'] ){
                    setcookie(md5(base_url()).'_user_logged',$this->CI->my_string->encode_cookie(json_encode(array(
                            'ten_nguoidung' => $user['ten_nguoidung'],
                            'matkhau' => $user['matkhau'],
                            'chuoi_mahoa' => $user['chuoi_mahoa'],
                        ))), time()+7*24*3600, '/');
                    return array(
                        'ma_nguoidung' => $user['ma_nguoidung'],
                        'ten_nguoidung' => $user['ten_nguoidung'],
                        'matkhau' => $user['matkhau'],
                    );
                }
            }
            return $cookie;
        }
        return NULL;


    }

}